package com.sys.gradepaysys.service.impl;

import com.sys.gradepaysys.mapper.EventMapper;
import com.sys.gradepaysys.model.Event;
import com.sys.gradepaysys.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class EventServiceImpl implements EventService {
    @Autowired
    EventMapper eventMapper;

    @Override
    public boolean addEvent(Event event) {
        Integer saveResult = eventMapper.save(event);
        if (saveResult != 1) {
            return false;
        }
        return true;
    }

    @Override
    public List<Event> getEventListIsDoing() {
        Event event = new Event();
        event.setIsDoing(1);
        List<Event> events = eventMapper.selectEventByCondition(event);
        return events;
    }

    @Override
    public Event getEventByEid(Integer eid) {
        Event event = new Event();
        event.setEid(eid);
        List<Event> events = eventMapper.selectEventByCondition(event);
        Event model = new Event();
        for (Event event1 : events) {
            model = event1;
        }
        return model;
    }

    @Override
    public boolean supportEvent(Integer eid, String support) {
        String arr[] = {eid.toString()};
        Event eventByEid = this.getEventByEid(eid);
        Integer supNum = eventByEid.getSupNum();
        Integer noSup = eventByEid.getNoSup();
        noSup += 1;
        supNum += 1;
        Event event = new Event();
        event.setEid(eid);
        if (support.equals("support")) {
            event.setSupNum(supNum);
        }
        if (support.equals("no")) {
            event.setNoSup(noSup);
        }
        Integer update = eventMapper.update(event);
        if (update != 1) {
            return false;
        }
        if (support.equals("support")) {
            return true;
        }
        if (support.equals("no")) {
            return false;
        }
        return true;
    }

    @Override
    public boolean supportStart(Integer eid) {
        Event event = new Event();
        event.setEid(eid);
        event.setIsDoing(1);
        Integer update = eventMapper.update(event);
        if (update != 1) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkStuEventSupport(Integer sid, Integer eid) {
        List<Map<String, String>> maps = eventMapper.selectStueList(sid, eid);
        if (maps.size() > 0) {
            return false;
        } else {
            Integer update = eventMapper.saveStue(sid, eid);
            if (update != 1) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<Event> getEventListPay() {
        String s = LocalDateTime.now().toString();
        Event model = new Event();
        model.setETime(s);
        List<Event> events = eventMapper.selectEventByCondition(model);
        List<Event> eventList = new ArrayList<>();
        for (Event event : events) {
            Integer total = event.getSupNum() + event.getNoSup();
            if (total != 0) {
                Double isSuccess = Double.valueOf(event.getSupNum() / total);
                if (isSuccess > (2 / 3)) {
                    eventList.add(event);
                }
            }
        }
        for (Event event : eventList) {
            Event event1 = new Event();
            event1.setIsOk(1);
            event1.setEid(event.getEid());
            eventMapper.update(event1);
        }
        model.setIsOk(1);
        List<Event> events2 = eventMapper.selectEventByCondition(model);
        return events2;
    }

    @Override
    public boolean StuToPay(String sid, String eid, String pay) {
        Event model = new Event();
        model.setEid(Integer.valueOf(eid));

        String arr[] = {sid, eid, pay, LocalDateTime.now().toString()};
        String sql = "insert into spay(sid,eid,pay,pdate)values(?,?,?,?)";
        Double total = 0.0;
        Double aDouble = Double.valueOf(pay);
        List<Event> events = eventMapper.selectEventByCondition(model);
        Event event2 = new Event();
        for (Event event : events) {
            event2 = event;
        }
        Double totalPay = event2.getTotalPay();
        total = totalPay + aDouble;
        Event event = new Event();
        event.setEid(Integer.valueOf(eid));
        event.setTotalPay(total);
        eventMapper.update(event);
        Integer integer = eventMapper.saveSpay(sid, eid, pay, LocalDateTime.now().toString());
        if (integer != 1) {
            return false;
        }
        return true;
    }

    @Override
    public List<Event> getEventListIsOk() {
        Event event = new Event();
        event.setIsOk(1);
        List<Event> events = eventMapper.selectEventByCondition(event);
        return events;
    }
    @Override
    public List<Map<String, String>>getIncomeList() {
        return eventMapper.getIncomeList();
    }
    @Override
    public List<Map<String, String>>getOutEventList() {
        return eventMapper.getOutEventList();
    }

    @Override
    public List<Event> getEventListNotDoing() {
        Event event = new Event();
        event.setIsDoing(0);
        List<Event> events = eventMapper.selectEventByCondition(event);
        return events;
    }

    @Override
    public List<Map<String, String>>getEventInfoStu(String eid) {
        return eventMapper.selectEventInfoStuList(eid);
    }

    
    @Override
    public List<Map<String, String>>getEventStu(String eid) {
        return eventMapper.selectEventStu(eid);

    }
    
}
